# !/usr/bin/env python
# -*- coding: utf-8 -*-

# @File   : pathutil.py
# @Time   : 2022/5/24 4:49 下午
# @Author : yujianhui
import os


def project_root_path() -> str:
    """
    获取项目根路径
    :return:
    """
    path=os.path.abspath(os.path.dirname(__file__))
    root=os.path.dirname(os.path.dirname(path))
    return root


def path_join(start: str, end: str) -> str:
    """
    拼接路径
    :param start: 项目根路径
    :param end: 项目下的文件/目录 路径
    :return:
    """
    if end.startswith('/'):
        end=end.removeprefix('/')
    return os.path.join(start, end)


def all_file_path(path: str, type: str) -> list:
    """
    获取路径下某一种类型的所有文件
    :param path_list: 绝对路径列表
    :param type: 文件类型，如xml
    :return:
    """
    file_list = []
    if not os.path.exists(path):
        raise FileExistsError('文件 {} 不存在'.format(path))

    # path是文件，以xml结尾
    if os.path.isfile(path) and path.endswith(type):
        file_list.append(path)
    # path是目录
    if os.path.isdir(path):
        for root, subDirs, files in os.walk(path):
            for file in files:
                if file.endswith(type):
                    file_list.append(path_join(root, file))
    return file_list


if __name__ == '__main__':
    # path1 = path_join(project_root_path(), 'cases/v1')
    # path2 = path_join(project_root_path(), 'cases/v2')
    # print(all_file_path(path1, 'xml'))
    pass